	/*
 * Created on Oct 7, 2003
 * StatusStatsAction
 * Local Projects Database
 * (c) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.struts.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.stats.StatsDB;
import ro.gateway.aida.stats.StatsHolder;
import ro.gateway.aida.stats.StatusPieChart;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.utils.BugTracker;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;

/**
 * @author Mihai Postelnicu
 * StatusStatsAction
 * 
 *  *  
 */
public class StatusStatsAction extends TilesAction {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			  HttpServletRequest request, HttpServletResponse response) {
			  try {
				  ServletContext sc = getServlet().getServletContext();
				PersistenceToken token = PersistenceManager.
										  tokenLookup(sc);
				  String mode = request.getParameter("mode");
				  StatsDB sdb = StatsDB.getManager(token);
				  Arbiter ar= Arbiter.getManager(token);
				  boolean show=false;
				  if (ar.canDo(request, Perms.SHOW_UNAPPROVED)) show=true; 
				  LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request,sc);
				  String lang = lang_bean.language;

			

				  StatsHolder sh = sdb.getStatusStats(lang,show);
				  if(sh.size()!=0) request.setAttribute("ststats", sh);

				  if ("widget".equals(mode)) {
					  return mapping.findForward("stats.txt.status.widget");
				  }

				  if ("pie".equals(mode)) {
					  StatusPieChart spc = new StatusPieChart(sh);
					  request.setAttribute("pieData", spc);

					  return mapping.findForward("stats.charts.status.pie");
				  }

				  if ("line".equals(mode)) {
					 // statsLineChart slc = new statsLineChart();
					  //request.setAttribute("pageViews", slc);

					  return mapping.findForward("stats.charts.status.line");
				  }

				  return mapping.findForward("stats.txt.status.widget");
			  } catch (Exception e) {
				BugTracker.publishBug(e, request);
				  return mapping.findForward("fail");
			  }
		  }
}
